<?php
	error_reporting(0);	
	date_default_timezone_set("Etc/GMT-8");
	include_once 'function.php';
	include_once 'db_config.php';
	writelog ( '开始执行发送邮件流程！' );
	
	$mailgroupid = $argv [1];
	$sendgroup = $argv [2];
	$delaytime = $argv [3];
	$send_id = $argv [4];
	$minms = $argv [5];
	$maxms = $argv [6];
	$sendn = $argv [7];
	$stopm = $argv [8];
	
	$conn = mysql_connect ($dbhost,$dbuser,$dbpwd);
	mysql_select_db ( $dbname );
	mysql_query ( 'set names utf8' );
	// 初始化fromEmail当天发送数为0
	$sql = 'update `crm_smtpsendermaillist` set `daytimes`=0 where `senddate`<>"' . date ( 'Y-m-d', time () ) . '"';
	mysql_query ( $sql );
	// 得到接收邮件清单
	$sql = 'select `mail_sendmail` mail,`user_name` name from `crm_smtpsendmalilist` where `send_state` in (2,3) and  `mailgroup_id` in (' . $mailgroupid . ')';
	$ret = @mysql_query ( $sql );
	$maillist = array ();
	$i = 0;
	while ( $row = @mysql_fetch_array ( $ret ) ) {
		$maillist [$i] ['name'] = $row ['name'];
		$maillist [$i ++] ['mail'] = $row ['mail'];
	}
	if (count ( $maillist ) < 1) {
		writelog ( '没有可用的收件人!', __FILE__, __LINE__ );
		exit ();
	}
	writelog ( '获取收件人邮件列表成功!' );
	// 获取发送内容和object
	try {
		$sql = 'select * from `crm_smtplog` where `id`=' . $send_id;
		$ret = @mysql_query ( $sql );
		$row = @mysql_fetch_array ( $ret );
		writelog ( '获取邮件内容成功!' );
	} catch ( Exception $e ) {
		writelog ( '获取收件人邮件列表失败!原因：' . $e );
		exit ();
	}
	$content = $row ['mail_content'];
	$subject = $row ['mail_object'];
	$sendret = doGroupSending ( $maillist, $subject, $content, $send_id, $sendgroup, $delaytime , $minms , $maxms , $sendn , $stopm);
	mysql_close ( $conn );